# 三合一。描述如何只用一个数组来实现三个栈。 
# 
#  你应该实现push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum)方法。
# stackNum表示栈下标，value表示压入的值。 
# 
#  构造函数会传入一个stackSize参数，代表每个栈的大小。 
# 
#  示例 1： 
# 
#  
#  输入：
# ["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
# [[1], [0, 1], [0, 2], [0], [0], [0], [0]]
#  输出：
# [null, null, null, 1, -1, -1, true]
# 说明：当栈为空时`pop, peek`返回-1，当栈满时`push`不压入元素。
#  
# 
#  示例 2： 
# 
#  
#  输入：
# ["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
# [[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
#  输出：
# [null, null, null, null, 2, 1, -1, -1]
#  
# 
#  
# 
#  提示： 
# 
#  
#  0 <= stackNum <= 2 
#  
# 
#  Related Topics 栈 设计 数组 👍 77 👎 0
from typing import List, Any


# leetcode submit region begin(Prohibit modification and deletion)
class TripleInOne:

    def __init__(self, stackSize: int):
        # 可以存入任意值，初始值为None的固定大小数组
        self.stack: List[Any] = [None] * (3 * stackSize)
        self.s = stackSize
        # 三个位置分别表示对应栈的长度
        self.pointers = [0, 0, 0]

    def push(self, stackNum: int, value: int) -> None:
        if self.pointers[stackNum] < self.s:
            index = stackNum * self.s + self.pointers[stackNum]
            self.stack[index] = value
            self.pointers[stackNum] += 1

    # 弹出栈顶元素
    def pop(self, stackNum: int) -> int:
        if self.pointers[stackNum] == 0:
            return -1
        else:
            index = stackNum * self.s + self.pointers[stackNum] - 1
            value = self.stack[index]
            self.stack[index] = None
            self.pointers[stackNum] -= 1
            return value

    # 查看栈顶元素
    def peek(self, stackNum: int) -> int:
        if self.pointers[stackNum] == 0:
            return -1
        else:
            index = stackNum * self.s + self.pointers[stackNum] - 1
            value = self.stack[index]
            return value

    def isEmpty(self, stackNum: int) -> bool:
        return self.pointers[stackNum] == 0

# Your TripleInOne object will be instantiated and called as such:
# obj = TripleInOne(stackSize)
# obj.push(stackNum,value)
# param_2 = obj.pop(stackNum)
# param_3 = obj.peek(stackNum)
# param_4 = obj.isEmpty(stackNum)
# leetcode submit region end(Prohibit modification and deletion)
